package cn.itsource.hander;

import cn.itsource.feignclient.FastdfsFeignClientt;
import cn.itsource.util.createfile.CreateFile;
import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import static cn.itsource.hrm.mq.Mq.QUEUE_NAME;

//消息接收者
@Component
public class Hander {
    @Autowired
    private FastdfsFeignClientt fastdfsFeignClientt;
    //监听sms队列
    @RabbitListener(queues = {QUEUE_NAME})
    public void receive_sms(String msg, Message message, Channel channel) throws IOException {
     //接收消息
        Map<String,String> map = JSON.parseObject(msg, Map.class);

        String physicalPath = map.get("physicalPath");//物理路径
        //下载路径
        String fastdfsPath = map.get("fastdfsPath");

        byte[] download = fastdfsFeignClientt.download(fastdfsPath);
        if(download.length>0&& download!=null){
            File file = CreateFile.Create(physicalPath);

            FileCopyUtils.copy(download,file);
            //触发手动签收  basicAck(long deliveryTag, boolean multiple)
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

        }
    }
}
